package com.patui.ImageProcessor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.util.Log;
public class ScaleImageProcessor implements ImageProcessor{
	private int 			mWidth;
	private int 			mHeight;
	private int 			mScaleType;
	private float 			scale;
	private float 			dx=0,dy=0;
	private final Matrix 	mMatrix=new Matrix();
	private final RectF  	mTempSrc=new RectF();
	private final RectF  	mTempDst=new RectF();
	public ScaleImageProcessor(int width, int height, int scaleType) {
		super();
		mWidth = width;
		mHeight = height;
		mScaleType = scaleType;
	}
	@Override
	public Bitmap processImage(Bitmap bitmap) {
		// TODO Auto-generated method stub
		if(bitmap==null)
		{
			return null;
		}
		mMatrix.reset();
        final int bWidth = bitmap.getWidth();
        final int bHeight = bitmap.getHeight();
        Log.e("debug", "<mWidth,mHeight>"+mWidth+":"+mHeight);
        Log.e("debug", "<bWidth,bHeight>"+bWidth+":"+bHeight);
        switch (mScaleType) {
		case ScaleType.CENTER_CROP:
	        if(bWidth*mHeight>mWidth*bHeight)
	        {
                scale = (float) mHeight / (float) bHeight;
                dx = (mWidth - bWidth * scale) * 0.5f;
	        }
	        else
	        {
	        	 scale = (float) mWidth / (float) bWidth;
                 dy = (mHeight - bHeight * scale) * 0.5f;
	        }
	        mMatrix.setScale(scale, scale);
            mMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
			break;
			
		case ScaleType.CENTER:
			dx = (int) ((mWidth - bWidth) * 0.5f + 0.5f);
			dy = (int) ((mHeight - bHeight) * 0.5f + 0.5f);
            mMatrix.setTranslate(dx, dy);
			break;
		case ScaleType.CENTER_INSIDE:
             if (bWidth <= mWidth && bHeight <= mHeight) {
                 scale = 1.0f;
             } else {
                 scale = Math.min((float) mWidth / (float) bWidth, (float) mHeight / (float) bHeight);
             }
             dx = (int) ((mWidth - bWidth * scale) * 0.5f + 0.5f);
             dy = (int) ((mHeight - bHeight * scale) * 0.5f + 0.5f);
             mMatrix.setScale(scale, scale);
             mMatrix.postTranslate(dx, dy);
			break;
		case ScaleType.FIT_XY:
		default:
            mTempSrc.set(0, 0, bWidth, bHeight);
            mTempDst.set(0, 0, mWidth, mHeight);
            mMatrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.FILL);
			break;
		}
        Bitmap result = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint=new Paint();
		paint.setAntiAlias(true);
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));	
        canvas.drawBitmap(bitmap, mMatrix, paint);
        return result;
	}

}
